class LessonsController < ApplicationController
layout 'standard'
before_filter :login_required, :authorize

  def authorize
    if @session['user'].role != "admin"
        redirect_back_or_default :controller => "error", :action => "info"
    end
  end


  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @lessons_menu = Lesson.find_by_sql("SELECT * FROM lessons,subjects WHERE lessons.subject_id = subjects.id ORDER BY subjects.name")
    
    sortby = params['sortby']
    
    case sortby
    when "subject"
        @lessons = Lesson.find_by_sql("SELECT * FROM lessons,subjects WHERE lessons.subject_id = subjects.id ORDER BY subjects.name")
    when "class_type"
        @lessons = Lesson.find(:all, :order => 'class_type_id,subject_id')
    when "lgrzaj"
        @lessons = Lesson.find(:all, :order => 'lgrzaj')
    when "lgrroz"
        @lessons = Lesson.find(:all, :order => 'lgrroz')    
    when "lgodznazaj"
        @lessons = Lesson.find(:all, :order => 'lgodznazaj')    
    when "semestr"
        @lessons = Lesson.find(:all, :order => 'semestr')    
    else 
        @lessons = Lesson.find(:all, :order => 'subject_id,semestr')
    end
  end


  def list_for_subject
    @lessons = Lesson.find(:all, :conditions => ["subject_id = ? ", params[:id]])
    @lessons_menu = @lessons
    sortby = params['sortby']
    case sortby
      when "class_type"
    @lessons = Lesson.find(:all, :conditions => ["subject_id = ? ", params[:id]], :order => 'class_type_id,subject_id')
      when "lgrzaj"
    @lessons = Lesson.find(:all, :conditions => ["subject_id = ? ", params[:id]], :order => 'lgrzaj')
      when "lgrroz"
    @lessons = Lesson.find(:all, :conditions => ["subject_id = ? ", params[:id]], :order => 'lgrroz')    
      when "lgodznazaj"
    @lessons = Lesson.find(:all, :conditions => ["subject_id = ? ", params[:id]], :order => 'lgodznazaj')    
      when "semestr"
    @lessons = Lesson.find(:all, :conditions => ["subject_id = ? ", params[:id]], :order => 'semestr')    
      else 
    @lessons = Lesson.find(:all, :conditions => ["subject_id = ? ", params[:id]], :order => 'subject_id,semestr')
    end
  end




  def show
    @lesson = Lesson.find(params[:id])
    @lessons_menu = Lesson.find_by_sql("SELECT * FROM lessons,subjects WHERE lessons.subject_id = subjects.id ORDER BY subjects.name")
  end

  def new
    @lesson = Lesson.new
    @lessons_menu = Lesson.find_by_sql("SELECT * FROM lessons,subjects WHERE lessons.subject_id = subjects.id ORDER BY subjects.name")
  end

  def create
    @lesson = Lesson.new(params[:lesson])
    @lessons_menu = Lesson.find_by_sql("SELECT * FROM lessons,subjects WHERE lessons.subject_id = subjects.id ORDER BY subjects.name")
    if @lesson.save
      flash[:notice] = 'Lesson was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @lesson = Lesson.find(params[:id])
    @lessons_menu = Lesson.find_by_sql("SELECT * FROM lessons,subjects WHERE lessons.subject_id = subjects.id ORDER BY subjects.name")
  end

  def update
    @lesson = Lesson.find(params[:id])
    @lessons_menu = Lesson.find_by_sql("SELECT * FROM lessons,subjects WHERE lessons.subject_id = subjects.id ORDER BY subjects.name")
    if @lesson.update_attributes(params[:lesson])
      flash[:notice] = 'Lesson was successfully updated.'
      redirect_to :action => 'show', :id => @lesson
    else
      render :action => 'edit'
    end
  end

  def destroy
    Lesson.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
